﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BingWallpaper.Common
{
    public static class AutoStart
    {

        //添加启动项

        /// <summary>
        /// 开机随系统启动
        /// </summary>
        public static void UnStart()
        {

            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run", true);

            if (run.GetValue("Bing壁纸") != null)
            {
                run.DeleteValue("Bing壁纸");
            }
            run.Close();
            hklm.Close();


        }

        /// <summary>
        /// 开机随系统启动
        /// </summary>
        public static void OnStart()
        { 
            RegistryKey hklm = Registry.LocalMachine;// Registry.LocalMachine; (本地)  CurrentUser（当前用户）
            //@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"  64位注册表  以管理员身份打开的项目 放到64位注册表打不开 需要放到32位的里面
            RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run", true);


            if (run.GetValue("Bing壁纸") == null)
            {
                string exeFileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;//含可执行文件名  
                run.SetValue("Bing壁纸", $"\"{exeFileName}\" -startup");
            }
            else
            {
                run.DeleteValue("Bing壁纸");
                string exeFileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;//含可执行文件名
                run.SetValue("Bing壁纸", $"\"{exeFileName}\" -startup");
            }
            run.Close();
            hklm.Close();
            // RefreshSystem();


        }


         

    }
}
